﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace pcsse.MasterPages
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected DataTable dt;
        const int Page_size = 30;//每页显示数目
        protected int Page_num;
        private void refresh_dt()
        {
            dt = db_news.getDBhandle().GetDataTableBySQL("SELECT * FROM T_CATEGORY");
            //DataList1.DataSource = dt;
            //DataList1.DataBind();
            //====================================
            int cur_page_num;
            //int curpage = Convert.ToInt32(lbPage.Text);
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dt.DefaultView;
            ps.AllowPaging = true; //是否可以分页 
            ps.PageSize = Page_size; //显示的数量 
            ps.CurrentPageIndex = 0; //取得当前页的页码
            DataList1.DataSource = ps;
            DataList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToInt32(Session["Priority"]) < 4) Response.Redirect("..\\");
            if (!Page.IsPostBack)
            {
                refresh_dt();
            }
        }

        public void BtnCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "BtnDel":
                    db_news.getDBhandle().DelCategoryById(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    break;
                case "BtnEdit":
                    category_data cate_data=new category_data();
                    cate_data.id=int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    TextBox tb1 = (TextBox)e.Item.FindControl("TB_Title");
                    cate_data.title = tb1.Text;
                    db_news.getDBhandle().UpdatCategory(cate_data);
                    break;
            }
            refresh_dt();
        }

        protected void BtnADD_Click(object sender, EventArgs e)
        {
            category_data new_cate = new category_data();
            new_cate.title = TextBox1.Text;
            db_news.getDBhandle().AddCategory(new_cate);
            TextBox1.Text = null;
            refresh_dt();
        }
    }
}
